package com.xuetang9.utils.sn;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类: ErrorMessageUtils
 * @描述:  错误工具类
 * @date: 2020/5/13
 * @author: Admin
 * @ver 1.0.0
 * @since JDK 1.8
 */
public final class ErrorMessageUtils {

    /**
     * 将验证错误信息封装成Map集合
     * @param bindingResult 错误信息
     * @return 信息集合
     */
    public static Map<String, String> getError(BindingResult bindingResult) {
        List<FieldError> fieldErrors =  bindingResult.getFieldErrors();
        Map<String,String> errorMap=new HashMap<>(fieldErrors.size());
        for (FieldError error:fieldErrors
             ) {
            String name = error.getField();
            String message = error.getDefaultMessage();
            errorMap.put(name, message);
        }
        return errorMap;
    }
}
